package org.vectrics.common.view;

import org.apache.ecs.html.Div;
import org.apache.ecs.html.Font;
import org.apache.ecs.html.IMG;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.util.StringUtils;
import org.vectrics.location.Location;
import org.vectrics.provider.CoreServiceLocator;

public class HtmlFormLocationPickerRow extends HtmlFormRow {
	
	public HtmlFormLocationPickerRow(HtmlForm form, String caption, String paramName, boolean required, int popupLeft, int popupTop) {
		super(form, paramName, required);
    	String inputId = "location_picker_" + paramName;
    	this.getHtmlForm().addHidden(paramName, this.getHtmlForm().getParameterValue(paramName));
    	this.getLabelDiv().addElement(caption);
		
    	Table table = new Table();
    	table.setCellPadding(0);
    	table.setCellSpacing(0);
    	Row innerRow = table.addRow();
    	Cell innerCell1 = innerRow.addCell();
    	Cell innerCell2 = innerRow.addCell();
    	this.getValueDiv().addElement(table);
    	
    	Div div = new Div();
    	div.setID(inputId);
    	div.setClass(this.getHtmlForm().getStaticClass());
    	innerCell1.addElement(div);
    	String locationId = this.getHtmlForm().getParameterValue(paramName);
    	if (StringUtils.isNullOrBlank(locationId) == false) {
    		Location location = CoreServiceLocator.getLocationService().findLocation(new Long(locationId));
    		Font font = new Font();
    		font.setClass("stdFont");
    		font.addElement(location.getName());
    		div.addElement(font);
    	}
    	
    	String typeCode = "job.location";
    	
	    // Button to pick location
    	IMG buttonImage = new IMG();
    	buttonImage.setSrc("/hrs-intranet/dynagraphic/button.jpg?code=button.date.pick&caption=Select");
    	buttonImage.setAlt("Pick Location");
    	buttonImage.setBorder(0);
    	buttonImage.setClass("imageButton");
    	buttonImage.setOnClick("javascript:locationSearchDiv.showLocationSearchDiv(" +
    			popupLeft + ", " +
    			popupTop + ", '" +
    			caption + "', '" +
    			paramName + "', '" +
    			typeCode  +	"' , '"+
    			"country' );");
    	innerCell2.addElement(buttonImage);
		
	}

}
